﻿using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NationalD3.Domain.Interfaces;

namespace NationalD3.Infrastructure.SMS
{
    /// <summary>
    /// 短信服务
    /// </summary>
    public class ShortMessage : IShortMessage
    {
        public string Send(string phoneNumber, string name)
        {
            //构建一个阿里云客户端, 用于发起请求。
            //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAI5tKb9Sk7hwTsxv5KGCzi", "rQPPRmX4RoELbpXxaXO8Jf8rIsSj34");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            //构建请求，设置请求参数
            CommonRequest request = new CommonRequest();
            //请求方法 方法类型为POST
            request.Method = MethodType.POST;
            //请求域名
            request.Domain = "dysmsapi.aliyuncs.com";
            //请求版本
            request.Version = "2017-05-25";
            //请求方法
            request.Action = "SendSms";
            //接收短信的手机号码
            request.AddQueryParameters("PhoneNumbers", $"{phoneNumber}");
            //短信签名名称
            request.AddQueryParameters("SignName", "森林阅读短信服务");


            //timeSpan用法//https://vimsky.com/examples/usage/timespan-fromseconds-method-in-c-sharp.html
            //_redi.Set(DoctorPhone, o, TimeSpan.FromSeconds(30));
            //_redi.Set(DoctorPhone, o, TimeSpan.FromSeconds(60 * 5));

            if (name == "" || name == null)
            { //短信模板CODE
                request.AddQueryParameters("TemplateCode", "SMS_255300223");
                var o = RandNumStr(4);//负责随机数调用
                string code = string.Format("{{\"code\":{0}}}", o);

                //短信模板变量对应的实际值，JSON格式
                request.AddQueryParameters("TemplateParam", o);
            }
            else
            {
                //短信模板CODE
                request.AddQueryParameters("TemplateCode", "SMS_256950798");

                request.AddQueryParameters("TemplateParam", "{\"name\":\"" + name + "\"}");
            }


            try
            {
                //提交请求
                CommonResponse response = client.GetCommonResponse(request);
                Console.WriteLine(Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                return "No";
            }
            catch (ClientException e)
            {
                return "NO";
            }
            return "Yes";

        }

        public static string RandNumStr(int length)
        {
            string str = "1234567890";
            Random r = new Random();
            string result = "";
            for (int i = 0; i < length; i++)
            {
                int m = r.Next(0, str.Length);
                string s = str.Substring(m, 1);
                result += s;
            }
            return result;
        }
    }
}
